package org.jeecg.modules.demo.jxcx.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.po.JxcStock;
import org.jeecg.modules.demo.jxcx.po.JxcSupplier;
import org.jeecg.modules.demo.jxcx.service.JxcStockService;
import org.jeecg.modules.demo.jxcx.service.JxcSupplierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * (JxcStock)表控制层
 *
 * @author makejava
 * @since 2024-07-23 17:29:01
 */
@RestController
@RequestMapping("jxcStock")
public class JxcStockController{
    private static Logger logger = LoggerFactory.getLogger(JxcStockController.class);
    /**
     * 服务对象
     */
    @Resource
    private JxcStockService jxcStockService;
    @Resource
    private JxcSupplierService jxcSupplierService;
    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        return new R().ok(jxcStockService.getByConpanyId(companyId));
    }
    @PostMapping("/likeall")
    @ResponseBody
    public R likeall(@RequestBody JSONObject params) {
        String like = params.getString("like");
        String companyId = params.getString("companyId");
        return new R().ok(jxcStockService.getBylike(like,companyId));
    }
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jxcStock 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JxcStock> page, JxcStock jxcStock) {
        return new R().ok(this.jxcStockService.page(page, new QueryWrapper<>(jxcStock)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return new R().ok(this.jxcStockService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jxcStock 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody JxcStock jxcStock) {
        this.add(jxcStock);
        return new R().ok(this.jxcStockService.save(jxcStock));
    }
    public void add(JxcStock jxcStock){
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        // 格式化当前的日期和时间
        String formattedDateTime = now.format(formatter);
        String id = now.format(formatter2);
//        id = "1"+String.valueOf((int)(Math.random()*900000+100000))+id;
        jxcStock.setId(id);
        jxcStock.setCreateTime(formattedDateTime);
        String seller = jxcStock.getSeller();
        String companyId = jxcStock.getCompanyId();
        String number = jxcStock.getNumber();
        String unitPrice = jxcStock.getUnitPrice();
        //用bigdecimal计算总额，小数点位数为0
        String totalfee = String.valueOf(new BigDecimal(number).multiply(new BigDecimal(unitPrice)));
        JxcSupplier jxcSupplier = jxcSupplierService.getBy(seller, companyId);
        if (jxcSupplier == null){
            JxcSupplier jxcSupplier1 = new JxcSupplier();
            jxcSupplier1.setId(id);
            jxcSupplier1.setCreateTime(formattedDateTime);
            jxcSupplier1.setName(seller);
            jxcSupplier1.setCompanyId(companyId);
            jxcSupplier1.setAmount(totalfee);
            jxcSupplierService.save(jxcSupplier1);
        }else {
            jxcSupplier.setAmount(new BigDecimal(jxcSupplier.getAmount()).add(new BigDecimal(totalfee)).toString());
            jxcSupplierService.updateById(jxcSupplier);
        }
        jxcStock.setAmount(totalfee);
    }
    /**
     * 修改数据
     *
     * @param jxcStock 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public R update(@RequestBody JxcStock jxcStock) {
        return new R().ok(this.jxcStockService.updateById(jxcStock));
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");
        logger.info("删除数据"+idList);
//        String traner = params.getString("traner");
//        String companyId = params.getString("companyId");
//        JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
        return new R().ok(this.jxcStockService.removeByIds(idList));
    }
}

